/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import com.transitionseverywhere.*;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.utils.LightweightMap;
import com.andkulikov.transitionseverywhere.ResourceTable;
import java.util.ArrayList;

public class PathMotionSample extends AbilitySlice  {
    private StackLayout mStackLayout;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        StackLayout layout = (StackLayout) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_slice_pathmotion_transition, null, false);
        mStackLayout = (StackLayout) layout.findComponentById(com.andkulikov.transitionseverywhere.ResourceTable.Id_dlViewRoot);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mStackLayout.setTag(tagMap);
        Button btnDoMagic = (Button) layout.findComponentById(ResourceTable.Id_btnDoMagic);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(0xFF0000DD));
        background_color_element.setBounds(0, 0, btnDoMagic.getRight(), btnDoMagic.getBottom());
        btnDoMagic.setBackground(background_color_element);
        Display displayMetrics = DisplayManager.getInstance().getDefaultDisplay(getContext()).get();
        int w = displayMetrics.getAttributes().width;
        int h = displayMetrics.getAttributes().height;
        System.out.println("BOttom" + w  + "    " + h);
        setUIContent(layout);
        btnDoMagic.setClickedListener(new Component.ClickedListener() {
            boolean mColorsInverted;
            @Override
            public void onClick(Component Component) {
                final Transition changeBounds = new ChangeBounds();
                changeBounds.setPathMotion(new ArcMotion());
                changeBounds.setDuration(5000);
                TransitionManager.beginDelayedTransition(mStackLayout, changeBounds);
                mColorsInverted = !mColorsInverted;
                ComponentContainer.LayoutConfig L2= new ComponentContainer.LayoutConfig();
                L2.setMargins(10,50,10,10);
                ComponentContainer.LayoutConfig L1= new ComponentContainer.LayoutConfig();
                L1.setMargins(w-400,h-400,10,10);
                btnDoMagic.setLayoutConfig(mColorsInverted?L1:L2);
            }
        });
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
